package com.wondersgroup.fjzlpt.tbxt.dao.impl.tz;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.dialect.DialectProxy;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.tz.SxzztzDao;
import com.wondersgroup.fjzlpt.tbxt.util.ControllerUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 家庭医生签约台账
 */
@Repository
public class SxzztzDaoImpl extends BaseJTDAOImpl implements SxzztzDao{

    @Autowired
    private DialectProxy dialectProxy;

    @Value("${common.WSGLJGLBDM}")
    private String GLJGLBDM;

    @Override
    @Autowired
    @Qualifier("TB_TBXT_SXZZTZ")
    public void setTb(TableBean tb){
        super.setTb(tb);
    }


    /**
     * 分页查询
     * @param map
     * @return
     */
    public JSONObject queryPage(Map map){
        ControllerUtils.pageFormat(map);
        JSONObject sqlObj = this.getPageSql(map);
        List sqlParams = (List)sqlObj.get("sqlParams");
        PageQuerySQLBean pageBean = super.checkAndCreatePageQuerySQLBean(map,sqlObj.getString("sql"),null,sqlParams.toArray());
        return super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
    }

    /**
     * 合成分页语句
     * @param map
     * @return
     */
    private JSONObject getPageSql(Map<String,String> map){
        JSONObject result = new JSONObject();

        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from ");
        sqlBuf.append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("where 1=1 ");

        List sqlParams = new ArrayList();
        String value = map.get("JGLB");
        if(!StringUtils.isEmpty(value) && value.indexOf(GLJGLBDM) != -1){
            //管理机构
            sqlBuf.append("and a.XZQHDM like ? ");
            sqlParams.add(map.get("XZQHDM") + "%");
        }else if(!StringUtils.isEmpty(value = map.get("PTJGID"))){
            //非管理机构-只查询本机构
            sqlBuf.append("and a.PTJGID = ? ");
            sqlParams.add(value);
        }
        //签约居民
        value = map.get("XM");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.XM like ? ");
            sqlParams.add("%" + value + "%");
        }
        //转诊类别（1：转出；2.转入）
        value = map.get("ZZBS");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.ZZBS = ? ");
            sqlParams.add(value);
        }
        //转诊类型
        value = map.get("ZZLX");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.ZZLX = ? ");
            sqlParams.add(value);
        }
        //转出时间段
        value = map.get("FQZZSJ_start");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.FQZZSJ >= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }
        value = map.get("FQZZSJ_end");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.FQZZSJ <= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }
        //转入时间段
        value = map.get("ZZYYSJ_start");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.ZZYYSJ >= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }
        value = map.get("ZZYYSJ_end");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.ZZYYSJ <= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }

        String groupFunName = dialectProxy.getRow2ColumnFuncName(this.getJdbcTemp().getDataSource());
        if("GROUP_CONCAT".equals(groupFunName.toUpperCase())){
            sqlBuf.append("order by a.GXSJ desc is not null,a.XZSJ desc,a.FQZZSJ desc,a.ZZYYSJ desc ");
        }else if("LISTAGG".equals(groupFunName.toUpperCase())){
            sqlBuf.append("order by a.GXSJ desc Nulls last,a.XZSJ desc,a.FQZZSJ desc,a.ZZYYSJ desc ");
        }

        result.put("sql",sqlBuf.toString());
        result.put("sqlParams",sqlParams);

        return result;
    }

    /**
     * 查询双向转诊页签数据总量
     * @param map
     * @return
     */
    public JSONObject queryCount(Map<String,String> map){
        JSONObject result = new JSONObject();

        StringBuffer sqlBuf = new StringBuffer();
        //ZCNUM:转出数量； ZRNUM：转入数量
        sqlBuf.append("select sum(case when a.ZZBS=1 then 1 else 0 end) ZCNUM,sum(case when a.ZZBS=2 then 1 else 0 end) ZRNUM from ");
        sqlBuf.append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("where 1=1 ");

        List sqlParams = new ArrayList();
        String value = map.get("JGLB");
        if(!StringUtils.isEmpty(value) && value.indexOf(GLJGLBDM) != -1){
            //管理机构
            sqlBuf.append("and a.XZQHDM like ? ");
            sqlParams.add(map.get("XZQHDM") + "%");
        }else if(!StringUtils.isEmpty(value = map.get("PTJGID"))){
            //非管理机构-只查询本机构
            sqlBuf.append("and a.PTJGID = ? ");
            sqlParams.add(value);
        }

        SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());

        return super.query(bean,ResultHandlerConstant.JSONOBJECT);
    }
}
